//BUZZER 3
//k1     4
//k2     5
//k3     6
//k4     7

#include <SPI.h>
#include <Chrono.h>
#include "RF24.h"
#include "SR04.h"

#define N 40
#define BUZZER_PIN  3
#define CE_PIN   9
#define CSN_PIN  10
#define K1_PIN   4
#define K2_PIN   5
#define K3_PIN   6
#define K4_PIN   7

Chrono Vibration;
bool radioNumber = 1;//设定模块地址
RF24 radio(CE_PIN, CSN_PIN);//RF模块的CE和CSN引脚。
byte addresses[][6] = {"1Node", "2Node"};// 用于2个节点通信的无线模块通道地址

void Key_Init() {
  pinMode(K1_PIN, INPUT);
  pinMode(K2_PIN, INPUT);
  pinMode(K3_PIN, INPUT);
  pinMode(K4_PIN, INPUT);
}

int Key_Read() {
  if (!digitalRead(K1_PIN)) {
    delay(10);
    if (!digitalRead(K1_PIN)) {
      return 1;
    }
  }
  if (!digitalRead(K2_PIN)) {
    delay(10);
    if (!digitalRead(K2_PIN)) {
      return 2;
    }
  }
  if (!digitalRead(K3_PIN)) {
    delay(10);
    if (!digitalRead(K3_PIN)) {
      return 3;
    }
  }
  if (!digitalRead(K4_PIN)) {
    delay(10);
    if (!digitalRead(K4_PIN)) {
      return 4;
    }
  }
  return 0;
}

unsigned int filter5( int pin ){
  unsigned int count, i, j, temp;
  unsigned int value_buf[N];
  int  sum = 0;
  for ( count = 0; count < N; count++ ) {
    value_buf[count] =  analogRead(pin);
    delay(5);
  }
  for ( j = 0; j < N - 1; j++ ) {
    for ( i = 0; i < N - j - 1; i++ ) {
      if ( value_buf[i] > value_buf[i + 1] ) {
        temp = value_buf[i];
        value_buf[i] = value_buf[i + 1];
        value_buf[i + 1] = temp;
      }
    }
  }
  for ( count = 1; count < N - 1; count++ ) {
    sum += value_buf[count];
  }
  return ( unsigned int )( sum / ( N - 2 ) );
}

void setup() {
  //Serial.begin(115200);// 定义串口监视器波特率
  //Serial.println("start");
  Key_Init();
  radio.begin();
  radio.enableAckPayload();// 允许在确认数据包上自定义有效数据
  radio.enableDynamicPayloads();// 确认有效数据是动态数据
  radio.openWritingPipe(addresses[radioNumber]);// 打开一个通过字节数组进行写入的通道
  pinMode(BUZZER_PIN, OUTPUT);
  delay(2000);//等待超声模块稳定
}

void loop(void) {
  if (Vibration.hasPassed(50)) { 
    //读取按键值
    byte value = Key_Read();
    //Serial.print("按键值:");
    //Serial.println(value);
    //需要发送
    if (value != 0) {
      bool gotByte;// 初始化传入响应的变量
      radio.stopListening();// 停止接收
      //Serial.write("现在发送");
      //Serial.println(value);
      bool judge = radio.write(&value, sizeof(byte));
      delay(20);//等待ACK
      if ( judge ) {// 将变量发送到另一个接收模块
        if (!radio.available()) {// 如果缓冲区里没有数据，得到一个空白的答复。
          //Serial.println("得到空白的答复");
        } 
        else {
          while (radio.available() ) {// 判断是否接收到有效数据
            radio.read( &gotByte, sizeof(bool) );// 读取并显示响应时间
            //Serial.print("得到响应");
            //Serial.println(gotByte);
            //Serial.println("");
          }
        }
      }
      else{
        //Serial.println("发送失败");// 如果没有ACK响应，发送失败
      }
    }
    Vibration.restart(); //重新启动crono，以便稍后再次触发
  }
  
  //获取距离信息
  int up_dis = filter5(1);//上方测距
  int down_dis = filter5(2);//下方测距
  up_dis = up_dis / 10;
  down_dis = down_dis / 10;
  //Serial.print("读取到的值:");
  //Serial.print(up_dis);
  //Serial.print(",");
  //Serial.println(down_dis);
  //digitalWrite(BUZZER_PIN,LOW);//不需要震动提示
  if (up_dis >= 14  || down_dis >= 14) {//报警阈值
    digitalWrite(BUZZER_PIN, HIGH); //需要震动提示
    //Serial.println("报警");
    delay(500);
  }
  else { //不需要发送，也无需提示
    digitalWrite(BUZZER_PIN, LOW); //不需要震动提示
  }
}
